package com.hengpeng.msls.controller.activity;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.hengpeng.common.configuration.ConfigProvider;
import com.hengpeng.common.configuration.ConfigProviderFactory;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.RewardRebate;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.msls.controller.BaseController;
import com.hengpeng.msls.util.WeixinUtil;
import com.hengpeng.msls.vo.SessionMember;

/**
 * 邀请返利控制类
 * @author lugaofei 20151026
 *
 */
@Controller
@RequestMapping(value="/rebate")
public class RewardRebateController extends BaseController {
    public final static ConfigProvider<String> cp = ConfigProviderFactory
            .getInstance(Constant.HP_COMMON_PROPERTIES_FILEPATH);

    public String appId = cp.getConfigByPrimaryKey("weixin.appId");

    public String appSecret = cp.getConfigByPrimaryKey("weixin.appSecret");
    
    public String h5domain = cp.getConfigByPrimaryKey("weixin.h5domain");
    
    /**
     * 返利列表页
     */
    @RequestMapping(value="listpage")
    public String listPage(HttpServletRequest request,ModelMap map){
        SessionMember session = getSessionMember(request);
        if (session == null) {
            return "/user/login/index.jsp";
        }
        UserInfo userInfo = session.getUser();
        String recommendCode = userInfo.getRecommendCode();
        Long inviteCount = serviceFactory.getActivityService().getFirstInviteCount(recommendCode);
        Long paidMoney = serviceFactory.getActivityService().getPaidMoney(userInfo.getId());
        Long unpayMoney = serviceFactory.getActivityService().getUnpayMoney(userInfo.getId());
        
        /**生成微信分享jssdk所需参数**/
        StringBuffer urlBuffer= request.getRequestURL();
        String url = urlBuffer.toString();
        if (appId != null && !"".equals(appId)) {
            /**
             * 获取accesstoken
             */
            String accessToken = WeixinUtil.getBaseAccessToken(appId, appSecret);
            System.out.println("----------"+appId+"*************"+appSecret);
            
            /**
             * 获取jsapiticket
             */
            String jsapiTicket = WeixinUtil.getBaseJsapiTicket(appId, accessToken);
            /**
             * 生成js 接口 签名信息
             */
            Map<String, String> jsapiValus = WeixinUtil.sign(jsapiTicket, url);
            map.put("appId", appId);
            map.put("url", jsapiValus.get("url"));
            map.put("nonceStr", jsapiValus.get("nonceStr"));
            map.put("timestamp", jsapiValus.get("timestamp"));
            map.put("signature", jsapiValus.get("signature"));
            
        }
        map.addAttribute("recommendCode", recommendCode);
        map.addAttribute("paidMoney", paidMoney);
        map.addAttribute("unpayMoney", unpayMoney);
        map.addAttribute("inviteCount", inviteCount);
        map.put("h5domain", h5domain);
        return "/page/account/reward/rewardrebate.jsp";
    }
    /**
     * 获取返利列表
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public void getRebateList(HttpServletRequest request,ModelMap map,String curPage,PrintWriter printWriter,HttpServletResponse response){
        SessionMember session = getSessionMember(request);
        Map<String , List<?>> dataMap = new HashMap<String, List<?>>();
        List<Integer> pagecount = new ArrayList<Integer>();
        int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
        UserInfo userInfo = session.getUser();
        String recommendCode = userInfo.getRecommendCode();
        map.addAttribute("recommendCode", recommendCode);
        ServiceResult<List<RewardRebate>> serviceResult = serviceFactory.getActivityService().findRewardRebate(userInfo.getId(), int_curPage, 10);
        pagecount.add(serviceResult.getReturnListSize());
        dataMap.put("list", serviceResult.getReturnValue());
        dataMap.put("pageCount", pagecount);
        Gson gson = new Gson();
        printWriter.write(gson.toJson(dataMap)); 
        printWriter.flush(); 
        printWriter.close();
    }
}
